package GUI;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.LayoutManager;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JPanel;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import database.TableManager;
import database.TableManager.TableEnum;

public class GUI_page05_InsertToTable extends JPanel {

	TableManager tm;
	
	JLabel titleLabel;
	JButton returnButton;
	List<String> tableOptions;
	JComboBox tableList;
	
	JPanel entryPanel = new JPanel(); // panel for dynamic generated fields
	List<JLabel> entryLabels;
	List<JTextField> entryFields;
	
	JButton insertButton;
	
	public GUI_page05_InsertToTable() {
		// get number of tables and names
		tableOptions = new ArrayList<String>();
		for (TableEnum table : TableEnum.values())
            tableOptions.add(table + "");
		
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		this.setBackground(Color.white);
		
		// title label
		titleLabel = new JLabel("Insert to Table", JLabel.CENTER);
		titleLabel.setVerticalTextPosition(JLabel.CENTER);
		
		titleLabel.setHorizontalTextPosition(JLabel.CENTER);
		titleLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
		titleLabel.setFont(new Font("Century Gothic", Font.BOLD, 20));
		titleLabel.setForeground(Color.gray);
		this.add(titleLabel);
        
		this.add(Box.createRigidArea(new Dimension(0,15)));

        // return button
		returnButton = GUI_AMSStoreMain.makeGrayWhiteButton("Cancel", Component.LEFT_ALIGNMENT);
		this.add(returnButton);
        
        this.add(Box.createRigidArea(new Dimension(0,10)));
        
        // list of tables to choose from, by default on first one
        Object[] tableStrings = tableOptions.toArray();
        tableList = new JComboBox(tableStrings);
        tableList.setSelectedIndex(0);
        tableList.setAlignmentX(Component.LEFT_ALIGNMENT);
        tableList.setForeground(Color.gray);
        tableList.setBackground(Color.white);
        tableList.setFont(new Font("Century Gothic", Font.PLAIN, 13));
        tableList.setMaximumSize(new Dimension(200,20));
        tableList.setSize(new Dimension(200, 20)); 
        this.add(tableList);
        
        this.add(Box.createRigidArea(new Dimension(0,10)));
        
		this.rePopulateFields();
	}
	
	public void rePopulateFields()
	{
		this.remove(entryPanel);
		entryFields = null;
		entryFields = new ArrayList<JTextField>();
		entryLabels = null;
		entryLabels = new ArrayList<JLabel>();
		entryPanel = new JPanel();
		entryPanel.setLayout(new BoxLayout(entryPanel, BoxLayout.Y_AXIS));
		entryPanel.setAlignmentX(LEFT_ALIGNMENT);
		entryPanel.setAlignmentY(TOP_ALIGNMENT);
		entryPanel.setMaximumSize(new Dimension(400, 500));
		entryPanel.setBackground(Color.white);
		
		tm = TableManager.getInstance();
		List<String> cols = tm.getTableColumnNames(TableEnum.valueOf(this.tableOptions.get(this.tableList.getSelectedIndex())));
		List<String> primaryCols = tm.getTablePrimaryKeyColumnNames(TableEnum.valueOf(this.tableOptions.get(this.tableList.getSelectedIndex())));

		for (int i = 0; i < cols.size();i++)
		{
			Boolean isPrimary = false;
			for (int j = 0; j < primaryCols.size(); j++)
			{
				if (cols.get(i).equals(primaryCols.get(j)))
				{
					isPrimary = true;
					break;
				}
			}
			JLabel currLabel = GUI_AMSStoreMain.makeLeftAlignedGrayLabel(cols.get(i), 11);
			if (isPrimary)
			{
				currLabel.setText("*" + currLabel.getText());
				currLabel.setForeground(Color.BLACK);
			}
			entryLabels.add(currLabel);
			JTextField currField = GUI_AMSStoreMain.makeLeftGrayTextField("", 300);
			entryFields.add(currField);

	        entryPanel.add(currLabel);
			entryPanel.add(currField);
			entryPanel.add(Box.createRigidArea(new Dimension(0,5)));
		}

        // insert button
		insertButton = GUI_AMSStoreMain.makeGrayWhiteButton("Insert into Table", Component.LEFT_ALIGNMENT);
		insertButton.setForeground(Color.RED);
		entryPanel.add(insertButton);
		this.add(entryPanel);
	}
}
